package com.ep.common.plugins;

import freemarker.template.TemplateException;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.*;

public class FormVueComponentGenerator {

    public void generate(IntrospectedTable introspectedTable) {
        generateVueComponent(introspectedTable);
    }

    private void generateVueComponent(IntrospectedTable introspectedTable) {
        try {
            // 获取表名和类名
            String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName();
            String componentName = CodeGenerationUtils.toCamelCase(tableName);
            String componentFileName = componentName + "-add-or-update.vue";

            // 配置 Freemarker
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
            cfg.setDirectoryForTemplateLoading(new File("src/main/resources/templates"));
            cfg.setDefaultEncoding("UTF-8");

            // 加载模板文件
            Template template = cfg.getTemplate("FormComponent.vue.ftl"); // 修改模板文件名

            // 准备数据模型
            Map<String, Object> data = new HashMap<>();
            List<Map<String, String>> columns = new ArrayList<>();

            // 动态生成表单字段配置
            for (IntrospectedColumn column : introspectedTable.getAllColumns()) {
                Map<String, String> columnData = new HashMap<>();
                columnData.put("name", column.getJavaProperty());
                columnData.put("label", column.getRemarks());
                columnData.put("type", getColumnType(column));
                columns.add(columnData);
            }

            data.put("columns", columns);
            data.put("camelCaseTableName", componentName);

            // 渲染模板
            File outputDir = new File("src/main/resources/generated");
            if (!outputDir.exists()) {
                outputDir.mkdirs();
            }

            try (Writer out = new FileWriter(new File(outputDir, componentFileName))) {
                template.process(data, out);
            }

            System.out.println("前端 Vue 组件代码生成完成！");
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
    }

    // 辅助方法：根据列类型返回对应的 Vue 组件类型
    private String getColumnType(IntrospectedColumn column) {
        String jdbcTypeName = column.getJdbcTypeName();

        switch (jdbcTypeName) {
            case "VARCHAR":
            case "CHAR":
                return "el-input";
            case "INTEGER":
            case "BIGINT":
                return "el-input-number";
            case "DATE":
            case "TIMESTAMP":
                return "el-date-picker";
            case "TEXT":
                return "el-input-textarea";
            default:
                return "el-input";
        }
    }
}